package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/1 17:16
 */
@RestController
public class DownLoadController {
    @RequestMapping("download")
    public void downLoad(String fileName, HttpServletResponse response) {
//        根据用户的请求确定需要下载那个文件,把文件保存客户端
        String path = "E:/";
        try {
            response.addHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
            IOUtils.copy(new FileInputStream(path + fileName), response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
